package com.gk.panda.apis.patrol.commons;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * @author xiongxiaojing
 * @version 创建时间：2020年8月7日 下午2:19:34
 */

public class EasypoiExcelUtils {

  private static void downLoadExcel(String fileName, HttpServletResponse response,
      Workbook workbook) throws Exception {
    ServletOutputStream outStream = null;
    try {
      response.setCharacterEncoding("UTF-8");
      response.setHeader("content-Type", "application/vnd.ms-excel");
      response.setHeader("Content-Disposition",
          "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
      outStream = response.getOutputStream();
      workbook.write(outStream);
    } catch (IOException e) {
      throw new RuntimeException(e);
    } finally {
      if (outStream != null) {
        outStream.close();
      }
    }
  }

  private static <T> void defaultExport(List<T> dataList, Class<?> clz, String fileName,
      HttpServletResponse response, ExportParams exportParams) throws Exception {
    Workbook workbook = ExcelExportUtil.exportExcel(exportParams, clz, dataList);
    if (workbook != null) {
      downLoadExcel(fileName, response, workbook);
    }
  }

  public static <T> void exportExcel(List<T> dataList, String title, String sheetName, Class<?> clz,
      String fileName, boolean isCreateHeader, HttpServletResponse response) throws Exception {
    ExportParams exportParams = new ExportParams(title, sheetName);
    exportParams.setCreateHeadRows(isCreateHeader);
    defaultExport(dataList, clz, fileName, response, exportParams);
  }

  public static <T> void exportExcel(List<T> dataList, String title, String sheetName, Class<?> clz,
      String fileName, HttpServletResponse response) throws Exception {
    defaultExport(dataList, clz, fileName, response, new ExportParams(title, sheetName));
  }
  
  /**
   * 多sheet页下载
   */
  public static void exportExcelSheet(List<Map<String, Object>> sheetsList,String fileName, HttpServletResponse response) throws Exception {
    Workbook workbook = ExcelExportUtil.exportExcel(sheetsList, ExcelType.HSSF);
    downLoadExcel(fileName, response, workbook);
  }

  private static void defaultExport(List<Map<String, Object>> dataList, String fileName,
      HttpServletResponse response) throws Exception {
    Workbook workbook = ExcelExportUtil.exportExcel(dataList, ExcelType.XSSF);
    if (workbook != null) {
      downLoadExcel(fileName, response, workbook);
    }
  }

  public static void exportExcel(List<Map<String, Object>> dataList, String fileName,
      HttpServletResponse response) throws Exception {
    defaultExport(dataList, fileName, response);
  }

  public static <T> List<T> importExcel(String filePath, Integer titleRows, Integer headerRows,
      Class<T> clz) throws Exception {
    if (StringUtils.isBlank(filePath)) {
      return null;
    }

    ImportParams params = new ImportParams();
    params.setTitleRows(titleRows);
    params.setHeadRows(headerRows);

    try {
      return ExcelImportUtil.importExcel(new File(filePath), clz, params);
    } catch (Exception e) {
      throw new RuntimeException(e.getMessage());
    }
  }

  public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows,
      Integer startRows, Class<T> clz) throws Exception {
    if (file == null) {
      return null;
    }

    ImportParams params = new ImportParams();
    params.setTitleRows(titleRows);
    params.setHeadRows(headerRows);
    params.setStartRows(startRows);

    try {
      return ExcelImportUtil.importExcel(file.getInputStream(), clz, params);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
}
